const express = require('express');
const cors = require('cors');
const fs = require('fs');
const bodyParser = require('body-parser');

const app = express();
const PORT = process.env.PORT || 3000;
const HOST = '0.0.0.0';

app.use(cors()); // 添加CORS中间件

app.use(bodyParser.json());

app.get('/file', (req, res) => {
    fs.readFile('data.txt', 'utf8', (err, data) => {
        if (err) {
            return res.status(500).send('Error reading file');
        }
        res.send(data);
    });
});

app.post('/file', (req, res) => {
    const newData = req.body.data;

    fs.writeFile('data.txt', newData, 'utf8', (err) => {
        if (err) {
            return res.status(500).send('Error writing file');
        }
        res.send('File updated successfully');
    });
});

app.listen(PORT,HOST, () => {
    console.log(`Server running at http://${HOST}:${PORT}/`);
});
